package com.zhousb.deviceManager.security;

import com.zhousb.deviceManager.model.entity.SysRole;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

@Data
public class JwtUser implements UserDetails {

    private Long userId;

    private String username;

    @JsonIgnore
    private String password;
    private Set<SysRole> roles;
    private String realName;
    private String userTel;
    private Integer isLock;
    private Long tenantId;

    public JwtUser(Long userId, String username, String password, Set<SysRole> roles, String realName, String userTel,Long tenantId) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.roles = roles;
        this.realName = realName;
        this.userTel = userTel;
        this.tenantId = tenantId;
    }

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<SimpleGrantedAuthority> authorities = new ArrayList<>();
        this.roles.forEach(role -> {
            authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getRoleCode()));
        });
        return authorities;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}
